import Vue from "vue";
import Vuex from "vuex";
import axios from "axios";
Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    goodsList: [],
  },
  mutations: {
    // 接受传递的数组给state中数据赋值
    initList(state, list) {
      state.goodsList = list;
    },
  },
  actions: {
    // 异步获取数据代码
    getList(context) {
      axios.get("/goods").then((data) => {
        // 把获取的数据赋值到mutations中的方法中
        context.commit("initList", data.data);
      });
    },
  },
  modules: {},
});
